package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.AdminDao;
import com.yc.jpyy.mobile.service.AdminService;


@Controller
@RequestMapping(value = "/admin")
public class GetAdminSystemReportController extends BaseController {
	
	@Autowired
	private AdminService adminService;

	@Autowired
	private AdminDao adminDao;
	private static Logger logger = LoggerFactory.getLogger(AdminHomePageController.class);
	
	
	
	/**
	 * 发布通知
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/publishNews", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String publishNews(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 用户d
			String userId = request.getParameter("userId");
			// 标题
			String title = request.getParameter("title");
			// 内容
			String content = request.getParameter("content");
			// 驾校ID
			String driverId = request.getParameter("driverId");
			// 通知对象
			String object = request.getParameter("object");
			
			if(userId != null && !userId.isEmpty()
					&& title != null && !title.isEmpty()
					&& content != null && !content.isEmpty()
					&& driverId != null && !driverId.isEmpty()
					&& object != null && !object.isEmpty()){
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("driverId", driverId);
				paramsMap.put("userId", userId);
				paramsMap.put("title", title);
				paramsMap.put("content", content);
				paramsMap.put("object", object);
				response = adminService.publishNews(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("缺少必填字段！");
			}
		} catch (Exception e) {
			logger.info("publishNews:"+ e.toString());
			response.setReturnMsg(0,"发布通知失败！");
		} finally {
			return response.toJSON();
		}
	}
	/**
	 * 获取通知
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/getAdminNews", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String getAdminNews(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 驾校ID
			String driverId = request.getParameter("driverId");
			String PageNum = request.getParameter("PageNum");
			String PageSize = request.getParameter("PageSize");

			if (PageNum != null && !PageNum.isEmpty() || PageSize != null
					&& !PageSize.isEmpty()) {
				if (PageNum.equals("0") || PageSize.equals("0")) {
					response.setErrorcode(1);
					response.setMessage("页码或页数不能为0...");
					return response.toJSON();
				}
			}
			if((null != driverId && !driverId.isEmpty()))
			{   
				Map<String,Object> paramsMap = new HashMap<>();
				paramsMap.put("PageNum", PageNum);
				paramsMap.put("PageSize", PageSize);
				paramsMap.put("driverId", driverId);
				//查询公告信息
				response = adminService.getNews(paramsMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("必填参数为空！");
			}
		} catch (Exception e) {
			logger.info("getAdminNews:"+ e.toString());
			response.setReturnMsg(0,"获取通知列表失败！");
		} finally {
			return response.toJSON();
		}
	}
	
}
